<template>
  <div class="login">
    <div class="login-bg"></div>
    <!--  login-->
    <div class="login-wrap" @keyup.enter="doLogin">
      <p class="login-text">用户登录</p>
      <el-input
          autofocus
          prefix-icon="el-icon-user"
          placeholder="请输入账号"
          v-model="user.username"
          size="mini"
          clearable>
      </el-input>
      <br>
      <br>
      <el-input
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
          v-model="user.password"
          size="mini"
          show-password>
      </el-input>
      <br>
      <br>
      <el-button type="primary" size="mini" style="width: 100%;" :loading="loading" @click="doLogin">登录</el-button>
    </div>
    <Copy/>
  </div>
</template>

<script>
import Copy from "@/components/layouts/Copy";
import {login} from "../api/login";

export default {
  name: "Login",
  components: {Copy},
  data() {
    return {
      user: {username: 'admin', password: '123456'},
      loading: false,
    }
  },
  created() {

  },
  methods: {
    //登录
    async doLogin() {
      if (!this.check()) {
        return false;
      }
      this.loading = true;
      const resp = await login(this.user)
      if (resp.code === 200) {
        this.$message.success('登录成功')
        sessionStorage.setItem("user", JSON.stringify(resp.data))
        await this.$router.push("/admin")
      } else {
        this.$message.error(resp.message)
      }
      this.loading = false;
    },
    //验证
    check() {
      const user = this.user;
      if (user.name === '') {
        this.$message.warning('请输入用户名')
        return false;
      }
      if (user.password === '') {
        this.$message.warning('请输入密码')
        return false;
      }
      return true;
    }
  }
}
</script>

<style scoped lang="scss">
.login-bg {
  //background-color: #0066A8;
  background: #fff url(../assets/login_back.jpg) no-repeat;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
}

.login-wrap {
  width: 220px;
  height: 220px;
  padding: 40px 50px 0 330px;
  background: #fff url(../assets/cars.jpg) no-repeat 30px 40px;
  margin: auto;
  overflow: hidden;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;

  .login-text {
    text-align: center;
    font-size: 13px;
    font-weight: 300;
  }
}
</style>